#!/bin/bash

export JAVA_HOME=${doris_admin_basic_setting_javahome}
export JBOSS_HOME=${doris_admin_basic_setting_jbosshome}
export APACHE_HOME=${doris_admin_basic_setting_apachehome}
export JETTY_HOME=${doris_admin_basic_setting_jettyhome}
export WEB_APP_HOME=${doris_admin_deploy}
export OUTPUT_HOME=${doris_admin_output}
export PRODUCTION_MODE=${doris_admin_basic_setting_mode}
#set ($app_port = $numberUtil.createInteger(${doris_admin_appserver_port}) )
#set ($debug_port = $app_port + 8 )
export APP_PORT=$app_port

DEBUG_PORT=$debug_port

export CHECK_STARTUP_URL="http://localhost:$app_port/ok.htm"
export STARTUP_SUCCESS_MSG="OK"

if [ ! -e $APACHE_HOME ]; then
    echo "********************************************************************"
    echo "**Error: $APACHE_HOME not exist."
    echo "********************************************************************"    
    exit 1
fi

if [ ! -e $JAVA_HOME ]; then
    echo "********************************************************************"
    echo "**Error: $JAVA_HOME not exist"
    echo "********************************************************************"    
    exit 1
fi

if [ ! -e $JBOSS_HOME ]; then
    echo "********************************************************************"
    echo "**Error: $JBOSS_HOME not exist."
    echo "********************************************************************"
    exit 1
fi

JAVA_OPTS_EXT=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_DEBUG_OPT=" -server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=${doris_admin_debug_port},server=y,suspend=n "
JAVA_MONITOR_OPTS=" -Dtrace.flag=true -Dtrace.output.dir=$OUTPUT_HOME/trace/ "
LOGIN_OPTS="-Dlogin.auth=false"

if [ $PRODUCTION_MODE = "run" ]; then
    #after the environment of online is all 64-bit,the below if and else judgement can be delete
    str=`file $JAVA_HOME/bin/java | grep 64-bit`
    if [ -n "$str" ]; then
        let memTotal=`cat /proc/meminfo |grep MemTotal|awk '{printf "%d", $2/1024 }'`
        if [ $memTotal -gt 2500 ]; then
            JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
        else
            JAVA_MEM_OPTS=" -server -Xmx1g -Xms1g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
        fi
    else
        JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
    fi
    
    if [ -f $OUTPUT_HOME/trace.flag ]; then
        JAVA_OPTS=" $JAVA_MEM_OPTS $JAVA_MONITOR_OPTS "
    else
        JAVA_OPTS=" $JAVA_MEM_OPTS "
    fi
         
elif [ $PRODUCTION_MODE = "test" ]; then
    JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
    JAVA_OPTS=" $JAVA_MEM_OPTS $JAVA_MONITOR_OPTS "
    
elif [ $PRODUCTION_MODE = "dev" ]; then
    #we should reduce resource usage on developing mode
    JAVA_MEM_OPTS=" -Xms64m -Xmx1024m -XX:MaxPermSize=128m "
    JAVA_OPTS=" $JAVA_MEM_OPTS $JAVA_DEBUG_OPT $JAVA_MONITOR_OPTS $LOGIN_OPTS "
    
else
    echo "********************************************************************"
    echo "**Error: $PRODUCTION_MODE should be only: run, test, dev"
    echo "********************************************************************"    
    exit 1
fi
